package com.hunanwenli.controller.login;

import com.hunanwenli.entity.CommentaryInfo;
import com.hunanwenli.entity.UserInfo;
import com.hunanwenli.service.userInfo.FeignUserInfoService;
import com.hunanwenli.tool.Tools;
import com.hunanwenli.service.login.FeignLoginService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author wanzhenyu
 * @date 2021/6/3 0:12
 */
@Controller
public class LoginController {
    @Autowired
    private FeignLoginService feignLoginService;
    @Autowired
    private FeignUserInfoService feignUserInfoService;
    private static final Logger logger = Logger.getLogger(LoginController.class);

    @RequestMapping(value = {"/","/login"})
    public String login(Model model, String name, HttpServletRequest request){
        request.getSession().setAttribute("name",name);
        String token = Tools.makeToken();
        model.addAttribute("token",token);
        request.getSession().setAttribute("token",token);
        String error = (String)request.getAttribute("error");
        model.addAttribute("error",error);
        return "login";
    }

    @RequestMapping(value = "/home",method = RequestMethod.POST)
    public String home(Model model, String token, String phone, String password, HttpServletRequest request){
        if (Tools.isNullOrEmpty(token)) {
            return "forward:/login";}
        if (Tools.isNullOrEmpty(password)||Tools.isNullOrEmpty(phone)){
            request.setAttribute("error","账号或密码不能为空");
            return "forward:/login";
        }
        boolean isToken = token.equals(request.getSession().getAttribute("token"));
        if (isToken){
            request.getSession().removeAttribute("token");
        }else {
            return "forward:/login";
        }
        Integer id = feignLoginService.selectUserId(phone);
        if (Tools.isNullOrEmpty(id)){
            request.setAttribute("error","账户不存在");
            return "forward:/login";
        }
        boolean truePwd = feignLoginService.isTruePwd(id, password);

        if (truePwd){
            request.getSession().setAttribute("uuid",phone);
            logger.debug(phone+"验证成功即将登录");
            return "forward:/my";
        }else {
            request.setAttribute("error", "请输入正确的账号或密码");
            return "forward:/login";
        }
    }

    @RequestMapping(value = "/my",method = {RequestMethod.POST,RequestMethod.GET})
    public String my(Model model,HttpServletRequest request,Integer start){
        String uuid = (String)request.getSession().getAttribute("uuid");
        if(uuid==null){
            model.addAttribute("error","由于异常错误未找到用户信息。请重新登录");
            return "forward:/login";
        }
        Integer uid = feignLoginService.selectUserId(uuid);
        UserInfo userInfo = feignLoginService.getUserInfoById(uid);
        model.addAttribute("userName",userInfo.getName()+",欢迎您");

        return "index";
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logout(HttpServletRequest request,Model model){
        Object uuid = request.getSession().getAttribute("uuid");
        if (uuid!=null){
        request.getSession().removeAttribute("uuid");
    }
        model.addAttribute("error","账号退出成功，感谢使用");
        return "login";
    }
}
